package com.ls.admin.service;

import com.ls.admin.mapper.AdminMapper;
import com.ls.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;


    /**
     * 管理员审核用户申请接口
     * */
    @Transactional
    public void auditInterface(CustomerAppInter customerAppInter,String adminName){
        Admin admin = adminMapper.getAdminByName(adminName);
        if(null!=admin){
            customerAppInter.setAdminId(admin.getId());
            adminMapper.auditInterface(customerAppInter);
        }


    }

    /**
     * 管理员审核注册表
     *
     * */
    public void auditZhuce(Customer customer){

        adminMapper.auditZhuce(customer);
    }

    /**
     *
     * 管理员查询自由接口
     * */
    public List<InterVo> getInterfaceList(){
        List<InterVo> interList = adminMapper.getInterfaceList();

        for (InterVo inter:interList) {
            List<InterRes> interRes=adminMapper.returnToParameterTable(inter.getId());
            inter.setReturnFormatList(interRes);
            List<InterReq> interReqs=adminMapper.requestParameterTable(inter.getId());
            inter.setRequestMethodList(interReqs);
        }
        return interList;
    }

        /**
         *
         * 管理员查询第三方接口
         * */
        public List<InterThirdparty> getThirdpartyList(){
            List<InterThirdparty> thirdpartyList = adminMapper.getThirdpartyList();

            return thirdpartyList;
        }

    /**
     * 管理员对自己接口的删除
     *
     * */
        public void deleteLocalInterface(InterVo inter){

            adminMapper.deleteLocalInterface(inter);
            adminMapper.deleteInterReq(inter);
            adminMapper.deleteInterRes(inter);
        }

        /**
         * 管理员添加自有接口
         * */
        public void addInterface(InterVo inter){
            adminMapper.addInterface(inter);
            List<InterVo> interfaceList = adminMapper.getInterfaceList();
            InterVo interVo = interfaceList.get(interfaceList.size() - 1);
            List<InterRes> returnFormatList = inter.getReturnFormatList();
            for (InterRes  returnFormat:returnFormatList) {
                returnFormat.setInterfaceId(interVo.getId());
                adminMapper.addInterRes(returnFormat);
            }

            List<InterReq> requestMethodList = inter.getRequestMethodList();
            for (InterReq request:requestMethodList) {
                request.setInterfaceId(interVo.getId());
                adminMapper.addInterReq(request);
            }
        }


    /**
     * 管理员对第三方接口的删除
     *
     * */
        public void deleteThirdPartyInterface(InterThirdparty interThirdparty){
            adminMapper.deleteThirdPartyInterface(interThirdparty);
        }

    /**
     * 管理员通过第三方接口id查询第三方接口
     * */
        public InterThirdparty getThirdpartyById(InterThirdparty interThirdparty){
            InterThirdparty thirdparty=adminMapper.getThirdpartyById(interThirdparty);
            return thirdparty;
        }

        /**
         *管理员添加第三方接口
         * */
        public int  addThirdparty(InterThirdparty interThirdparty){
            return adminMapper.addThirdparty(interThirdparty);

        }


    /**
     * 管理员修改第三方接口
     *
     * */
        public void modifyThirdPartyInterface(InterThirdparty interThirdparty){

            adminMapper.modifyThirdPartyInterface(interThirdparty);
        }

    /**
     * 管理员通过自有id查询自有接口
     * */
    public InterVo getInterfaceById(InterVo inter){
        InterVo interVo=adminMapper.getInterfaceById(inter.getId());
        List<InterRes> interRes=adminMapper.returnToParameterTable(inter.getId());
        interVo.setReturnFormatList(interRes);
        List<InterReq> interReqs=adminMapper.requestParameterTable(inter.getId());
        interVo.setRequestMethodList(interReqs);
        return interVo;
    }

    /**
     * 管理员修改自有的接口
     *
     * */
    public void modifyOwnInterface(InterVo interVo){
        //修改请求参数表
        List<InterReq> requestMethodList = interVo.getRequestMethodList();
        for (InterReq request:requestMethodList) {
            adminMapper.updateInterReq(request);
        }

        //修改返回参数表
        List<InterRes> returnFormatList = interVo.getReturnFormatList();
        for (InterRes returnFormat:returnFormatList) {
            adminMapper.updateInterRes(returnFormat);
        }
        //修改自有接口表
        adminMapper.updateInter(interVo);

    }

    public List<CustomerAppInter> getAppInter() {
        return adminMapper.getAppInter();
    }

    public List<VerifiedPersonal> getVeriPers() {
        return adminMapper.getVeriPers();
    }

    public List<VerifiedCompany> getVeriComp() {
        return adminMapper.getVeriComp();
    }

    @Transactional
    public void auditPerson(String appid) {
        //修改个人认证表
        adminMapper.updateVerPer(appid);
        //修改用户表
        adminMapper.updateCust(appid,2);
    }

    @Transactional
    public void auditComp(String appid) {
        //修改企业认证表
        adminMapper.updateVerCom(appid);
        //修改用户表
        adminMapper.updateCust(appid,1);
    }

    @Transactional
    public InterVo getInterfaceDetail(Integer id) {
        InterVo interVo = adminMapper.getInterfaceById(id);
        List<InterReq> interReqs = adminMapper.getInterReq(id);
        List<InterRes> interReson = adminMapper.getInterRes(id);
        interVo.setRequestMethodList(interReqs);
        interVo.setReturnFormatList(interReson);
        return interVo;
    }

    public int addreq(InterReq interReq) {
        return adminMapper.addreq(interReq);
    }

    public int addres(InterRes interRes) {
        return adminMapper.addres(interRes);
    }

    public int delreq(Integer id) {
        return adminMapper.delreq(id);
    }

    public int delres(Integer id) {
        return adminMapper.delres(id);
    }

    public int addInter(Inter inter) {
        return adminMapper.addInter(inter);
    }
}
